/**
 * 客户端检测
 */
window.onload = function () {
  browserVersionControl();
};

function detectClient() {
  try {
    const engine = {
      ie: 0,
      gecko: 0,
      webkit: 0,
      khtml: 0,
      opera: 0,
      ver: null,
    };
    const browser = {
      ie: 0,
      firefox: 0,
      konq: 0,
      opera: 0,
      chrome: 0,
      safari: 0,
      ver: null,
    };
    let cpu = null;
    const ua = navigator.userAgent;
    // 判断系统类型32位/64位
    const agent = ua.toLowerCase();
    if (agent.indexOf("win32") > -1 || agent.indexOf("wow32") > -1) {
      cpu = 32;
    } else if (agent.indexOf("win64") > -1 || agent.indexOf("wow64") > -1) {
      cpu = 64;
    }
    if (window.opera) {
      engine.ver = window.opera.version();
      engine.opera = parseFloat(engine.ver);
    } else if (/AppleWebKit\/(\S+)/.test(ua)) {
      engine.ver = RegExp.$1;
      engine.webkit = parseFloat(engine.ver);
      if (/Chrome\/(\S+)/.test(ua)) {
        browser.ver = RegExp.$1;
        browser.chrome = parseFloat(browser.ver);
      } else if (/Version\/(S+)/.test(ua)) {
        browser.ver = RegExp.$1;
        browser.safari = parseFloat(browser.ver);
      } else {
        let safariVersion = 1;
        if (engine.webkit < 100) {
          safariVersion = 1;
        } else if (engine.webkit < 312) {
          safariVersion = 1.2;
        } else if (engine.webkit < 412) {
          safariVersion = 1.3;
        } else {
          safariVersion = 2;
        }
        browser.safari = browser.ver = safariVersion;
      }
    } else if (/KHTML\/(\S+)/.test(ua) || /Konqueror\/([^;]+)/.test(ua)) {
      engine.ver = RegExp.$1;
      engine.khtml = parseFloat(engine.ver);
    } else if (/rv:([^)]+)\) Gecko\/\d{8}/.test(ua)) {
      engine.ver = RegExp.$1;
      engine.gecko = parseFloat(engine.ver);
      if (/Firefox\/(\S+)/.test(ua)) {
        browser.ver = RegExp.$1;
        browser.firefox = parseFloat(browser.ver);
      }
    } else if (/MSIE ([^;]+)/.test(ua)) {
      engine.ver = browser.ver = RegExp.$1;
      engine.ie = browser.ie = parseFloat(engine.ver);
    }
    browser.ie = engine.ie;
    browser.opera = engine.opera;
    return {
      engine,
      browser,
      cpu,
    };
  } catch (err) {}
}

function versionFilter() {
  if (arguments.length === 1 && typeof arguments[0] === "string") {
    const version = arguments[0];
    const start = version.indexOf(".");
    if (start > 0) {
      const end = version.indexOf(".", start + 1);
      if (end !== -1) {
        return version.substr(0, end);
      }
    }
    return version;
  } else if (arguments.length === 1) {
    return arguments[0];
  }
  return 0;
}

function browserVersionControl() {
  try {
    const client = detectClient();
    // const downloadURL = {
    //   64: {
    //     chrome: "https://mirror.gree.com/#/Chrome/doc",
    //     firefox: "http://10.2.24.136/chrome/Firefox64_76.0.exe",
    //   },
    //   32: {
    //     chrome: "https://mirror.gree.com/#/Chrome/doc",
    //     firefox: "http://10.2.24.136/chrome/Firefox32_76.0.exe",
    //   },
    // };
    let msg = "";
    if (
      navigator.userAgent.toLowerCase().indexOf("trident") > 0 ||
      window.ActiveXObject !== undefined
    ) {
      msg =
        "本系统不支持IE内核的浏览器，如用IE内核的浏览器会有部分功能受限导致显示不正常，请用谷歌98以上浏览器!";
    }
    if (client.browser.chrome && versionFilter(client.browser.ver) < 98) {
      msg =
        "您当前的浏览器chrome内核 " +
        versionFilter(client.browser.ver) +
        " 版本过低，为了不影响系统的使用，建议使用谷歌98以上浏览器！";
    }
    if (client.browser.firefox && versionFilter(client.browser.ver) < 98) {
      msg =
        "您当前的浏览器firefox内核 " +
        versionFilter(client.browser.ver) +
        " 版本过低，为了不影响系统的使用，建议使用谷歌98以上浏览器！";
    }
    // if (client.browser.ie > 0) {
    //   alert('您当前的浏览器ie版本过低，为了不影响系统的使用，建议更新！')
    // }
    if (client.browser.safari && versionFilter(client.browser.ver) < 2) {
      msg =
        "您当前的浏览器safari " +
        versionFilter(client.browser.ver) +
        " 版本过低，为了不影响系统的使用，建议更新！";
    }
    if (client.browser.konq && versionFilter(client.browser.ver) < 60) {
      msg =
        "您当前的浏览器konq " +
        versionFilter(client.browser.ver) +
        " 版本过低，为了不影响系统的使用，建议更新！";
    }
    if (client.browser.opera && versionFilter(client.browser.ver) < 40) {
      msg =
        "您当前的浏览器opera " +
        versionFilter(client.browser.ver) +
        " 版本过低，为了不影响系统的使用，建议更新！";
    }
    // 如果浏览器不兼容，给出提示
    if (msg) {
      let div =
        '<div style="width:100%;height:100%;position: fixed;top:0;left:0;z-index:99999;background-color:rgba(0,0,0,0.3)">' +
        '<div style="width: 450px; border: 1px solid #aaa; padding: 20px; margin: 250px auto 0; background-color: white;-webkit-box-shadow: 0 3px 6px rgba(100,100,100,0.4);-moz-box-shadow: 0 3px 6px rgba(100,100,100,0.4);box-shadow: 0 3px 6px rgba(100,100,100,0.4);">' +
        '<div style="font-weight: bold; margin-bottom: 15px;text-align: left;"><span>警告:</span><img id="closeImg" style="float: right; cursor: pointer; width: 16px;height: 16px;" src="./close.png" alt=""></div>' +
        '<div style="font-size: 14px; line-height: 20px;">' +
        '<div style="margin-bottom: 10px; text-align: left;">' +
        msg +
        "</div>";
      div +=
        '      <div style="margin-bottom: 8px; text-align: left;">请在桌面管家下载安装谷歌或火狐浏览器的最新版本</div>';
      div +=
        '<div style="margin-top: 15px; text-align: right;"><span id="btn" style="display: inline-block; padding: 4px 25px; border: 1px solid #aaa; font-size: 14px; cursor: pointer;">确定</span></div></div></div></div>';

      const dEle = document.createElement("div");
      dEle.innerHTML = div;
      dEle.id = "browser-warning";
      document.body.appendChild(dEle);
      // document.body.innerHTML = div
      if (document.attachEvent) {
        document.getElementById("closeImg").attachEvent("onclick", clearDom);
        document.getElementById("btn").attachEvent("onclick", clearDom);
      } else {
        document.getElementById("closeImg").addEventListener("click", clearDom);
        document.getElementById("btn").addEventListener("click", clearDom);
      }
      // if (!!(window.attachEvent && !window.opera)) {
      //   document.execCommand('stop')
      // } else {
      //   window.stop()
      // }
    }
  } catch (err) {}

  function clearDom() {
    document.body.removeChild(document.getElementById("browser-warning"));
    // document.body.innerHTML = ''
  }
}
